<?php

namespace Admin\Controller;

use Common\Controller\AdminController;

class FoodProductController extends AdminController {

    public function index() {
        $account_user_id = $_SESSION['account_user_id'];
        $where['shopaccountid'] = $account_user_id;
        $name = $_REQUEST['name'];
        $onsale = $_REQUEST['onsale'];
        if (isset($name) && !empty($name)) {
            $where['name'] = array('like', "%" . $name . "%");
            $param['name'] = $name;
        }
        if ($onsale != '') {
            $where['onsale'] = $onsale == 2 ? 0 : $onsale;
            $param['onsale'] = $onsale;
        }
        $productModel = new \Admin\Model\ProductsModel();
        $result = $productModel->getList($where, $param);
        $this->result = $result['list'];
        $this->page = $result['page'];
        if (IS_AJAX) {
            $this->ajaxReturn($this->fetch("FoodProduct/cate_ajax"));
        } else {
            $this->display("FoodProduct/cate");
        }
    }

    // 获取一条菜品信息
    public function getFindProduct() {
        $account_user_id = $_SESSION['account_user_id'];
        $where['shopaccountid'] = $account_user_id;
        $id = $_REQUEST['id'];
        if (!isset($id) && empty($id)) {
            // 报错
        }
        $where['id'] = $id;
        $productModel = new \Admin\Model\ProductsModel();
        $reuslt = $productModel->getFind($where);

        if (!isset($result) && empty($result)) {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '非法参数', 'data' => $result));
        }
//        $this->ajaxReturn(array('code'=>10000,'msg'=>'请求成功','data'=>$result));
//        $this->result = $result;
//        $this->display();
    }

    public function addFoodProduct() {
        $dictsModel = new \Admin\Model\DictsModel();
        $productStuffModel = new \Admin\Model\ProductStuffModel();
        $producrCateModel = new \Admin\Model\ProductCateModel();
        if (!IS_POST) {
            if (!empty($_GET['id'])) {
                $where['id'] = $_GET['id'];
                $productModel = new \Admin\Model\ProductsModel();
                $productStuff = new \Admin\Model\ProductStuffModel();
                $result = $productModel->getFind($where);
                $class = $producrCateModel->getFind(array('productid' => $_GET['id']));

                $this->class = $class;
                $this->find = $result;
            }

            $dictsList = $dictsModel->getList(array('category' => 'fenlei', 'shopaccountid' => $_SESSION['account_user_id'], 'inuse' => 1));
            $disParam = $dictsModel->getList(array('category' => 'foodConfig', 'inuse' => 1, 'shopaccountid' => $_SESSION['account_user_id']));
            $this->disParam = $disParam;
            $this->dictsList = $dictsList;
            $this->display("FoodProduct/add");
            die;
        }
        $upload = new \Think\Upload(); // 实例化上传类
        $upload->maxSize = 3145728; // 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
        $upload->rootPath = './Upload/foodproduct/'; // 设置附件上传根目录
        // 上传文件 
        $info = $upload->upload();
        $account_user_id = $_SESSION['account_user_id'];
        $where['shopaccountid'] = $account_user_id;
        $name = $_POST['name'];
        $cover = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME'] . "/Upload/foodproduct/" . $info['img']['savepath'] . $info['img']['savename'];
        $content = $_POST['content'];
        $price = $_POST['price'];
        $discount = $_POST['discount'];
        $realprice = $_POST['realprice'];
        $onsale = $_POST['onsale'] == 1 ? 1 : 0;
        $onlinetype = $_POST['onlinetype'];
        $ispackage = $_POST['ispackage'] == 1 ? 1 : 0;
        $memo = $_POST['memo'];
        $inuse = $_POST['inuse'] == 1 ? 1 : 0;
        $classid = $_POST['classid'];

        $data['name'] = $name;
        $data['cover'] = $cover;
        $data['price'] = $price * 100;
        $data['discount'] = $discount;
        $data['content'] = $content;
        $data['realprice'] = $price * 100;
        $data['memo'] = $memo;
        $data['inuse'] = $inuse;
        $data['ispackage'] = $ispackage;
        $data['onsale'] = $onsale;
        $data['shopaccountid'] = $account_user_id;
        $data['onlinetype'] = $onlinetype;
        $data['createdt'] = date("Y-m-d H:i:s");
        $data['updatedt'] = date("Y-m-d H:i:s");
        $productModel = new \Admin\Model\ProductsModel();

//        $this->ajaxReturn($_POST);
        $foodConfig = json_decode($_POST['foodConfig'], true);

        // 更新
        if (!empty($_POST['id'])) {
            unset($data['createdt']);
            $where['id'] = $_POST['id'];
            if (empty($info)) {
                unset($data['cover']);
            }
            $result = $productModel->saveData($where, $data);
            // 查询改商品分类是否更改
            $mapWhere['productid'] = $_POST['id'];
            $flag = $producrCateModel->getFind($mapWhere);
            if ($flag['dictid'] != $classid) {
                $producrCateModel->delData($mapWhere);
                $mapData['productid'] = $_POST['id'];
                $mapData['dictid'] = $classid;
                $mapData['inuse'] = 1;
                $mapData['value'] = 1;
                $mapData['shopaccountid'] = $_SESSION['account_user_id'];
                $mapData['createdt'] = date("Y-m-d H:i:s");
                $producrCateModel->addData($mapData);
            }
            // 菜品配置
            $productStuffModel->delData(array('productid' => $_POST['id']));
        } else { // 新增
            $result = $productModel->addData($data);
            $mapData['productid'] = $result;
            $mapData['dictid'] = $classid;
            $mapData['inuse'] = 1;
            $mapData['shopaccountid'] = $_SESSION['account_user_id'];
            $mapData['createdt'] = date("Y-m-d H:i:s");
            $producrCateModel->addData($mapData);
        }
        foreach ($foodConfig as $key => $val) {
            $datas[$key]['productid'] = empty($_POST['id']) ? $result : $_POST['id'];
            $datas[$key]['dictid'] = $val['id'];
            $datas[$key]['value'] = $val['val'];
            $datas[$key]['shopaccountid'] = $account_user_id;
            $datas[$key]['createdt'] = date("Y-m-d H:i:s");
        }
        $productStuffModel->addDataAll($datas);
        if ($result) {
            $this->ajaxReturn(array("code" => 1, 'msg' => '请求成功', 'data' => array()));
        } else {
            $this->ajaxReturn(array("code" => 1, 'msg' => '请求错误', 'data' => array()));
        }
    }

    // 新增分类
    public function addClass() {
        $account_user_id = $_SESSION['account_user_id'];
        $name = $_POST['name'];
        $data['code'] = "code";
        $data['value'] = $name;
        $data['category'] = "fenlei";
        $data['createdt'] = date("Y-m-d H:i:s");
        $data['inuse'] = 1;
        $data['shopaccountid'] = $account_user_id;
        $dictsModel = new \Admin\Model\DictsModel();
        $result = $dictsModel->addData($data);
        if ($result) {
            $this->ajaxReturn(array('code' => 10000, 'msg' => '请求成功', 'data' => array()));
        } else {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '新增失败', 'data' => array()));
        }
    }

    public function getProductConfig() {
        $productStuffModel = new \Admin\Model\ProductStuffModel();
        $id = $_POST['productid'];
        $foodConfig = $productStuffModel->getList(array('b.category' => 'foodConfig', 'a.shopaccountid' => $_SESSION['account_user_id'], 'b.inuse' => 1, 'productid' => $_POST['id']));
        $this->ajaxReturn(array('code' => 10000, 'msg' => '请求成功', 'data' => $foodConfig));
    }

    // 材料预估
    public function materialBudget() {
        $date = empty($_POST['date']) ? 1 : $_POST['date'];
        $param['date'] = $date;
        if ($date == 1) {
            $startDT = date("Ymd") . "000000";
            $endDT = date("Ymd") . "235959";
        } else {
            $startDT = date("Ymd", time() + 3600 * 24) . "000000";
            $endDT = date("Ymd", time() + 3600 * 24) . "235959";
        }
        // 查询今日取货订单下所有商品
        $orderModel = new \Admin\Model\OrdersModel();
        $where['shopaccountid'] = $_SESSION['account_user_id'];
        $where['pickuptime'] = array('between', array($startDT, $endDT));
        $where['gorderstatus'] = 1;
        $where['ordertype'] = 0;
        $productList = $orderModel->getProductList($where);
        $productStuffModel = new \Admin\Model\ProductStuffModel();
        $a = 0;
        foreach ($productList['list'] as $key => $val) {
            $pwhere['productid'] = $val['productid'];
            $productUnit = $productStuffModel->getListForBudget($pwhere);
            foreach ($productUnit as $k => &$v) {
                $v['count'] = $v['unit'] * $val['count'];
                $newArr[$a] = $v;
                $a++;
            }
        }
        $newArr = $this->dataGroup($newArr, 'name');
        $a = 0;
        foreach ($newArr as $ke => $va) {
            $endArr[$a]['name'] = $ke;
            $count = 0;
            foreach ($va as $kk => $vv) {
                $count += $vv['count'];
            }
            $endArr[$a]['count'] = $count;
            $a++;
        }
        // 数组分页
        $count = count($endArr);
        $Page = new \Think\Page($count, 10, $param);
        $show = $Page->show();
        $list = array_slice($endArr, $Page->firstRow, $Page->listRows);

        $this->assign('list', $list);
        $this->assign('page', $show);
        if (IS_AJAX) {
            $this->ajaxReturn($this->fetch("FoodProduct/materialBudget_ajax"));
        } else {
            $this->display();
        }
    }

    public function getProductCate() {
        $account_user_id = $_SESSION['account_user_id'];
        $dictsModel = new \Admin\Model\DictsModel();
        $result = $dictsModel->getList(array('category' => 'fenlei', 'shopaccountid' => $account_user_id));
        if ($result) {
            $this->ajaxReturn(array('code' => 10000, 'msg' => '请求成功', 'data' => $result));
        } else {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '暂无分类', 'data' => $result));
        }
    }

    public function getCateProduct() {
        $id = $_POST['id'];
        $productModel = new \Admin\Model\ProductsModel();
        if ($id != 'a' && !empty($id)) {
            $where['dictid'] = $id;
        }
        $where['a.shopaccountid'] = $_SESSION['account_user_id'];
        $result = $productModel->getCateProduct($where);
        if ($result) {
            $this->ajaxReturn(array('code' => 10000, 'msg' => '请求成功', 'data' => $result));
        } else {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '暂无菜品', 'data' => $result));
        }
    }

    public function material() {
        $account_user_id = $_SESSION['account_user_id'];
        $dictsModel = new \Admin\Model\DictsModel();
        $result = $dictsModel->getListPage(array('category' => "foodConfig", 'shopaccountid' => $account_user_id));

        $this->list = $result['list'];
        $this->page = $result['page'];
        if (IS_AJAX) {
            $this->ajaxReturn($this->fetch("FoodProduct/material_ajax"));
        } else {
            $this->display();
        }
    }

    public function addMaterial() {
        $account_user_id = $_SESSION['account_user_id'];
        $dictsModel = new \Admin\Model\DictsModel();
        if (!IS_POST) {
            if (!empty($_GET['id'])) {
                $find = $dictsModel->getFind(array('id' => $_GET['id']));
                $this->find = $find;
            }
            $this->display();
        } else {
            $data['value'] = $_POST['name'];
            $data['inuse'] = $_POST['inuse'];
            $data['category'] = 'foodConfig';
            $data['shopaccountid'] = $account_user_id;

            if (!empty($_POST['id'])) {
                $where['id'] = $_POST['id'];
                $result = $dictsModel->saveData($where, $data);
            } else {
                $data['createdt'] = date("Y-m-d H:i:s");
                $result = $dictsModel->addData($data);
            }
            if ($result) {
                $this->ajaxReturn(array('code' => 10000, 'msg' => '请求成功', 'data' => array()));
            } else {
                $this->ajaxReturn(array('code' => 10001, 'msg' => '请求错误', 'data' => array()));
            }
        }
    }

    // 分类管理
    public function catelist() {
        $account_user_id = $_SESSION['account_user_id'];
        $dictModel = new \Admin\Model\DictsModel();
        $where['shopaccountid'] = $account_user_id;
        $where['category'] = 'fenlei';
        $cate = $dictModel->getList($where);
        $this->cate = $cate;
        $this->display();
    }

    public function saveInuse() {
        $account_user_id = $_SESSION['account_user_id'];
        $dictModel = new \Admin\Model\DictsModel();
        $id = $_POST['id'];
        $inuse = $_POST['inuse'];
        $cate = $_POST['cate'];
        
        $where['id'] = $id;
        $where['shopaccountid'] = $account_user_id;
        if(!empty($inuse) && isset($inuse)){
            $data['inuse'] = $inuse;
        }
        if(!empty($cate) && isset($cate)){
            $data['value'] = $cate;
        }
        
        $result = $dictModel->saveData($where, $data);
        if ($result) {
            $this->ajaxReturn(array('code' => 10000, 'msg' => '请求成功', 'data' => array()));
        } else {
            $this->ajaxReturn(array('code' => 10001, 'msg' => '请求错误', 'data' => array()));
        }
    }

    protected function dataGroup($dataArr, $keyStr) {
        $newArr = [];
        foreach ($dataArr as $k => $val) {    //数据根据日期分组
            $newArr[$val[$keyStr]][] = $val;
        }
        return $newArr;
    }

    public function test() {
        dump($_SERVER);
    }

}
